package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.dao.FootSociatyBfinfoDao;
import org.footballmanager.pandafootball.system.domain.FootBfinfo;
import org.footballmanager.pandafootball.system.domain.FootSociaty;
import org.footballmanager.pandafootball.system.domain.FootSociatyBfinfo;
import org.footballmanager.pandafootball.system.domain.FootSociatyInfo;
import org.footballmanager.pandafootball.system.domain.enums.BFLX;
import org.footballmanager.pandafootball.system.domain.enums.BFZT;
import org.footballmanager.pandafootball.system.form.impl.FootSociatyBfinfoSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootSociatyBfinfoMapper;
import org.footballmanager.pandafootball.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class FootSociatyBfinfoServiceImpl extends ServiceImpl<FootSociatyBfinfo> implements FootSociatyBfinfoService {
    private static final String ZHUTEAM = "zhuTeam";
    private static final String KETEAM = "keTeam";
    private static final String MYVIEW = "/hy/footSociatyBfinfo/myView";

    @Autowired
    private FootSociatyBfinfoDao footSociatyBfinfoDao;
    @Autowired
    private FootSociatyBfinfoMapper footSociatyBfinfoMapper;
    @Autowired
    private FootSociatyService footSociatyService;
	@Autowired
	private FootSociatyInfoService footSociatyInfoService;
    @Autowired
    private FootUserPersonService footUserPersonService;
    @Autowired
    private FootZnxService footZnxService;

    /**
     * 根据公会主键判定是否够3人，不够不能报分
     * @param zdId 工会主键
     * @return 是否能够报分
     */
	@Override
	public boolean isCanBf(Long zdId) {
		List<FootSociatyInfo> list = footSociatyInfoService.findSociatyBySociatyId(zdId);
		return null != list && list.size() >= 3;
	}

    /**
     * 报分
     *
     * @param zdId1 工会主键1
     * @param zdId2 工会主键2
     * @param jq1   进球1
     * @param jq2   进球2
     */
    @Override
    public void bf(Long zdId1, Long zdId2, Integer jq1, Integer jq2, HttpServletRequest request) {
        FootSociatyBfinfo bf = new FootSociatyBfinfo();
        bf.setBfTime(new Date());
        bf.setBfzt(BFZT.NOT);
        bf.setZd1(zdId1);
        bf.setZd2(zdId2);
        bf.setJq1(jq1);
        bf.setJq2(jq2);
        this.save(bf, request);
    }

    /**
     * 翻页方法
     *
     * @param footSociatyBfinfoSearchForm 条件
     * @return 列表
     */
    @Override
    public List<FootSociatyBfinfo> findall(FootSociatyBfinfoSearchForm footSociatyBfinfoSearchForm) {
        return footSociatyBfinfoMapper.findall(footSociatyBfinfoSearchForm);
    }

    /**
     * 根据主键获取记录
     * @param id 主键
     * @return 记录
     */
    @Override
    public FootSociatyBfinfo get(Serializable id) {
        return footSociatyBfinfoMapper.findById(id);
    }

    /**
     * 查询
     *
     * @param entityname                  实体类名
     * @param map                         参数
     * @param footSociatyBfinfoSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootSociatyBfinfoSearchForm footSociatyBfinfoSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footSociatyBfinfoSearchForm);
        Page<FootSociatyBfinfo> page = PageHelper.startPage(footSociatyBfinfoSearchForm.getPageIndex(), footSociatyBfinfoSearchForm.getPageSize());
        List<FootSociatyBfinfo> list = this.findall(footSociatyBfinfoSearchForm);
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footSociatyBfinfoSearchForm 条件
     * @param request                     请求
     */
    @Override
    public void deleteAll(FootSociatyBfinfoSearchForm footSociatyBfinfoSearchForm, HttpServletRequest request) {
        if(footSociatyBfinfoSearchForm.getSelectIds() != null){
            String[] result = footSociatyBfinfoSearchForm.getSelectIds().split(Constants.COMMA);
            for(String id: result){
                this .delete(Long.parseLong(id), request);
            }
        }
    }

    /**
     * 查看详情
     *
     * @param id                          主键
     * @param map                         参数
     * @param request                     请求
     * @param footSociatyBfinfoSearchForm 条件
     * @param isViewPage                  是否是查看页面
     * @return 页面
     */
    @Override
    public String view(Long id, Model map, HttpServletRequest request, FootSociatyBfinfoSearchForm footSociatyBfinfoSearchForm, boolean isViewPage) {
        FootSociatyBfinfo bf = footSociatyBfinfoDao.selectByPrimaryKey(id);
        map.addAttribute(ZHUTEAM, footUserPersonService.findFootPersonByUserId(bf.getZd1(), request));
        map.addAttribute(KETEAM, footUserPersonService.findFootPersonByUserId(bf.getZd2(), request));
        map.addAttribute("footSociatyBfinfo", bf);
        return "hy/footSociatyBfinfo/" + (isViewPage ? "View" : "MyView");
    }

    /**
     * 确认报分
     *
     * @param footSociatyBfinfo 实体类
     * @param request           请求
     */
    @Override
    public void makesure(FootSociatyBfinfo footSociatyBfinfo, HttpServletRequest request) {
        if(footSociatyBfinfo.getId() != null){
            FootSociatyBfinfo bf = footSociatyBfinfoDao.selectByPrimaryKey(footSociatyBfinfo.getId());
            bf.setBfzt(BFZT.HAVE);
            FootSociaty zd2 = footSociatyService.get(footSociatyBfinfo.getZd2());
            footZnxService.sendZnx(footSociatyBfinfo.getZd2(), footSociatyBfinfo.getZd1(), "已确认战队赛比分",
                    zd2.getLogo(), MYVIEW, request);
            this.updateAll(bf, request);
        }
    }

    @Override
    @Transactional
    public int updateAll(FootSociatyBfinfo t, HttpServletRequest request) {
        log.info("更新球队报分:" + t.toString());
        footSociatyService.addEncourage(t, request);
        return super.updateNotNull(t, request);
    }

    @Override
    @Transactional
    public int delete(Serializable id, HttpServletRequest request) {
        FootSociatyBfinfo t = this.get(id);
        log.info("删除球队报分:" + t.toString());
        footSociatyService.deleteEncourage(t, request);
        return super.delete(id, request);
    }
}
